<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时不可以修改本文件，影响升级覆盖
 */

class Daohang extends \Phpcmf\Home\Module
{

    public function index() {
        // $this->_module_init();//
		// 导航接口
		$daohang_data = \Phpcmf\Service::M()->table('1_quan_category')->where('show', 1)->getAll();
        //循环导航，导航分别有一级 和 二级 导航
        $daohang = [];
        foreach ($daohang_data as $key => $val) {
            // 一级导航
            if ($val['pid'] == 0) {
                $daohang[$val['id']] = $val;
                // 二级导航
                $daohang[$val['id']]['son'] = [];
                foreach ($daohang_data as $k => $v) {
                    if ($v['pid'] == $val['id']) {
                        // 在栏目增加一个全部的导航
                        
                        $daohang[$val['id']]['son'][0]['name'] = '全部';
                        $daohang[$val['id']]['son'][0]['id'] = $val['id'];
                        $daohang[$val['id']]['son'][0]['child'] = $val['child'];
                        $daohang[$val['id']]['son'][0]['childids'] = $val['childids'];
                        
                        $daohang[$val['id']]['son'][$v['id']] = $v;
                    }
                }
            }
        }
        // $daohang 输出json数据
        //循环 
        $daohang2 = [];
        $daohang2[] = [
            'id' => 0,
            'name' => '全部',
            'son' => []
        ];
        foreach ($daohang_data as $key => $val) {
            // 一级导航
            if ($val['pid'] == 0) {
                $item = [
                    'id' => $val['id'],
                    'name' => $val['name'],
                    'son' => []
                ];
                // 添加"全部"选项
                $item['son'][] = [
                    'id' => $val['id'],
                    'name' => '全部'
                ];
                //$daohang2[$val['id']]['son'] = [];
                foreach ($daohang_data as $k => $v) {
                    if ($v['pid'] == $val['id']) {
                        // 在栏目增加一个全部的导航
                        $item['son'][] = [
                            'id' => $v['id'],
                            'name' => $v['name']
                        ];

                       
                    }
                }
                $daohang2[] = $item;
               
               
            }
        }
         // 
         $data = [
            'lisi1' => $daohang, 
            'lisi2' => $daohang2,
         ];

        $this->_json('1', '读取成功', $data);
        
    }

}
